/**
 * Copyright 2006 Brad Reynolds
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.leeloo.databinding.internal.ui;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.viewers.TableViewer;
import org.leeloo.core.Tag;
import org.leeloo.databinding.ui.TagFilterManager;

/**
 * Invokes a {@link TagSelectionDialog} to allow the user to manage filters.
 * <p>
 * Methods with category of "testability" only exist for testing purposes.
 * </p>
 * 
 * @author Brad Reynolds
 */
public class ManageFilterAction extends Action {
    /**
     * Viewer to filter.
     */
    private final TableViewer tableViewer;

    /**
     * Manager to handle maintaining tags and selection.
     */
    private final TagFilterManager tagFilterManager;

    /**
     * Constructs a new instance.
     * 
     * @param tableViewer viewer to apply filters to after the user has dismissed the dialog
     * @param tagFilterManager
     */
    public ManageFilterAction(TableViewer tableViewer, TagFilterManager tagFilterManager) {
        if (tableViewer == null) {
            throw new IllegalArgumentException("Parameter tableViewer was null."); //$NON-NLS-1$
        }
        if (tagFilterManager == null) {
            throw new IllegalArgumentException("Parameter tagFilterManager was null."); //$NON-NLS-1$
        }
        
        this.tableViewer = tableViewer;
        this.tagFilterManager = tagFilterManager;

        setText(Messages.getString("ManageFilterAction.select_filters")); //$NON-NLS-1$
    }

    /**
     * Opens a dialog and on OK sets the user entered state to the {@link #tagFilterManager}.
     * 
     * @see org.eclipse.jface.action.Action#run()
     */
    public void run() {
        TagSelectionDialog dialog = doCreateDialog(tableViewer, tagFilterManager);

        if (dialog.open() == TagSelectionDialog.OK) {
            tagFilterManager.clear();

            Tag[] tags = dialog.getTags();
            for (int i = 0; i < tags.length; i++) {
                tagFilterManager.addTag(tags[i]);
            }
            
            tagFilterManager.setSelection(dialog.getSelection());
        }
    }

    /**
     * Constructs the dialog to manage filters.
     * 
     * @param tableViewer 
     * @param tagFilterManager 
     * @return dialog 
     * @category testability
     */
    protected TagSelectionDialog doCreateDialog(TableViewer tableViewer, TagFilterManager tagFilterManager) {
        return new TagSelectionDialog(tableViewer.getControl().getShell(),
                tagFilterManager.getTags(),
                tagFilterManager.getSelection());
    }
}